package com.zf.shuashua.ui.popup;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxj.xpopup.core.CenterPopupView;
import com.zf.shuashua.R;
import com.zf.shuashua.api.Store;
import com.zf.shuashua.widget.MultiScrollNumber;

/**
 * 首页泡泡弹框
 */
public class ButtlePopup extends CenterPopupView {

    private TextView tvSum, tvTitle;
    private ImageView imgClose;
    private ImageView tmgGuideDouble;
    private LinearLayout doubleLayout;
    private MultiScrollNumber scrollNumber;
    private String value;
    private int multiple;
    private OnPopupListener mListener;

    public ButtlePopup(@NonNull Activity context, String value, int multiple) {
        super(context);
        this.value = value;
        this.multiple = multiple;
    }

    public void setPopupListener(OnPopupListener listener) {
        this.mListener = listener;
    }

    public void close() {
        dismiss();
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.buttle_popup;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        setupView();
        setupData();
    }

    private void setupView() {
        tvTitle = findViewById(R.id.tvTitle);
        tvSum = findViewById(R.id.tvSum);
        imgClose = findViewById(R.id.img_close);
        doubleLayout = findViewById(R.id.layout_double);
        scrollNumber = findViewById(R.id.scroll_number);
        tmgGuideDouble = findViewById(R.id.tmgGuideDouble);
        if (!Store.getInstance().getGuideDouble()) {
            tmgGuideDouble.setBackgroundResource(R.drawable.home_gold_guide_click_list);
            AnimationDrawable animal = (AnimationDrawable) tmgGuideDouble.getBackground();
            animal.start();
        }
        doubleLayout.setOnClickListener(v -> {
            Store.getInstance().setGuideDouble(true);
            if (mListener != null) {
                mListener.onNext();
            }
        });
        imgClose.setOnClickListener(v -> {
            dismiss();
            if (mListener != null) {
                mListener.onClose();
            }
        });
    }


    private void setupData() {
        tvTitle.setText("翻倍奖励");
        tvSum.setText(value);

        scrollNumber.postDelayed(() -> {
            if (scrollNumber != null) {
                scrollNumber.setNumber(1, multiple);
                scrollNumber.setTextSize(20);
                scrollNumber.setInterpolator(new DecelerateInterpolator());
            }
        }, 400);


//        String title = "看视频领取" + multiple + "倍奖励";
//        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(title);
//        RelativeSizeSpan span = new RelativeSizeSpan(1.5f);
//        spannableStringBuilder.setSpan(span, 5, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//        btnTitle.setText(spannableStringBuilder);
    }

}

